<template>
  <div @mouseenter="hovering = true"
       @mouseleave="hovering = false"
       style="display: flex"
  >
    <span v-if="label!==''"
          class="input-label"
          :style="{backgroundColor:labelColor}"
    >{{ label }}</span>
    <input type="text"
           v-bind="$attrs"
           :value="value"
           class="milk-input"
           :class="{'has-button':showButton,'has-label':label!==''}"
           @input="handleInput">
    <button class='milk-btn'
            v-if="showButton"
            @click="$emit('click')">{{ btn }}
    </button>
  </div>
</template>

<script>
export default {
  name: 'MilkInput',
  props: {
    value: [String, Number],
    btn: {
      type: String,
      default: '搜 索'
    },
    label: {
      type: String,
      default: ''
    },
    labelColor: {
      type: String,
      default: '#2e3236'
    },
    showButton: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    handleInput (event) {
      this.$emit('input', event.target.value)
    }
  }
}
</script>

<style>
/*input.vue*/
.milk-input {
  flex: 1;
  height: 38px;
  font-size: 14px;
  padding: 0 14px;
  outline: none;
  border: 1px solid #FFFFFF;
  border-radius: 4px;
  box-shadow: 0 0 2rem 0 rgba(136, 152, 170, .15);
  overflow: hidden;
}

.milk-input.has-button {
  border-radius: 4px 0 0 4px;
  border-right: none;
}

.milk-input.has-label {
  border-radius: 0 4px 4px 0;
  border-left: none;
}

.milk-input::-webkit-input-placeholder {
  color: #cbcbcb;
  font-size: 14px;
}

.milk-input:focus {
  border-color: #769b94;
}

.milk-btn {
  flex: 0 0 100px;
  cursor: pointer;
  background-color: #a175ed;
  color: white;
  font-size: 14px;
  outline: none;
  border: none;
  padding: 0 34px;
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
}

.milk-btn:hover {
  background-color: #c5a3ff;
}

.input-label {
  flex: 0 0 80px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  cursor: pointer;
  color: white;
  font-size: 14px;
  outline: none;
  border: none;
  padding: 0 18px;
  border-radius: 4px 0 0 4px;
}
</style>
